#!/bin/bash

# Tiny QA - 快速推送到阿里云容器镜像服务
# 支持命令行参数配置

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 默认配置
LOCAL_IMAGE="tiny-qa:latest"
REGISTRY="registry.cn-hangzhou.aliyuncs.com"
NAMESPACE=""
REPO_NAME="tiny-qa"
TAG="latest"

# 显示帮助信息
show_help() {
    echo -e "${BLUE}用法:${NC}"
    echo -e "  $0 -n <命名空间> [-t <标签>] [-r <仓库名>]"
    echo ""
    echo -e "${BLUE}参数:${NC}"
    echo -e "  -n, --namespace   阿里云命名空间 (必需)"
    echo -e "  -t, --tag         镜像标签 (默认: latest)"
    echo -e "  -r, --repo        仓库名称 (默认: tiny-qa)"
    echo -e "  -h, --help        显示帮助信息"
    echo ""
    echo -e "${BLUE}示例:${NC}"
    echo -e "  $0 -n your-username"
    echo -e "  $0 -n your-username -t v1.0.0"
    echo -e "  $0 -n your-username -t v1.0.0 -r my-tiny-qa"
}

# 解析命令行参数
while [[ $# -gt 0 ]]; do
    case $1 in
        -n|--namespace)
            NAMESPACE="$2"
            shift 2
            ;;
        -t|--tag)
            TAG="$2"
            shift 2
            ;;
        -r|--repo)
            REPO_NAME="$2"
            shift 2
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo -e "${RED}❌ 未知参数: $1${NC}"
            show_help
            exit 1
            ;;
    esac
done

# 检查必需参数
if [[ -z "$NAMESPACE" ]]; then
    echo -e "${RED}❌ 错误: 命名空间是必需的${NC}"
    show_help
    exit 1
fi

# 完整的远程镜像名称
REMOTE_IMAGE="${REGISTRY}/${NAMESPACE}/${REPO_NAME}:${TAG}"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Tiny QA - 快速推送到阿里云${NC}"
echo -e "${BLUE}========================================${NC}"
echo -e "${YELLOW}🔧 推送配置:${NC}"
echo -e "  本地镜像: ${LOCAL_IMAGE}"
echo -e "  远程镜像: ${REMOTE_IMAGE}"
echo -e "  命名空间: ${NAMESPACE}"
echo -e "  仓库名称: ${REPO_NAME}"
echo -e "  镜像标签: ${TAG}"
echo ""

# 检查本地镜像是否存在
if ! docker images | grep -q "tiny-qa.*latest"; then
    echo -e "${RED}❌ 本地镜像 ${LOCAL_IMAGE} 不存在${NC}"
    echo -e "${YELLOW}💡 请先构建镜像:${NC}"
    echo -e "  ./scripts/docker-build-stable.sh"
    exit 1
fi

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo -e "${RED}❌ Docker未运行，请先启动Docker Desktop${NC}"
    exit 1
fi

echo -e "${YELLOW}🚀 开始推送镜像...${NC}"

# 给镜像打标签
echo -e "${YELLOW}📦 给镜像打标签...${NC}"
docker tag "${LOCAL_IMAGE}" "${REMOTE_IMAGE}"
echo -e "${GREEN}✅ 镜像标签完成${NC}"

# 登录阿里云容器镜像服务
echo -e "${YELLOW}🔐 登录阿里云容器镜像服务...${NC}"
docker login "${REGISTRY}"

# 推送镜像
echo -e "${YELLOW}📤 推送镜像到阿里云...${NC}"
docker push "${REMOTE_IMAGE}"

echo ""
echo -e "${GREEN}🎉 镜像推送成功！${NC}"
echo -e "${BLUE}📋 镜像信息:${NC}"
echo -e "  镜像地址: ${REMOTE_IMAGE}"
echo -e "  仓库地址: https://cr.console.aliyun.com/cn-hangzhou/instances/repositories"
echo ""

# 显示拉取和运行命令
echo -e "${BLUE}📥 拉取命令:${NC}"
echo -e "  docker pull ${REMOTE_IMAGE}"
echo ""
echo -e "${BLUE}🚀 运行命令:${NC}"
echo -e "  docker run -d -p 5002:5002 --name tiny-qa ${REMOTE_IMAGE}"
echo ""

# 清理本地标签
echo -e "${YELLOW}🧹 清理本地标签...${NC}"
docker rmi "${REMOTE_IMAGE}"
echo -e "${GREEN}✅ 本地标签已清理${NC}"

echo -e "${GREEN}✅ 推送流程完成！${NC}" 